#!/usr/bin/env python

###
# Wifi + GPS fingerprint collection tool
# Copyright (c) 2008, Yura Zenevich, Jorge Silva, and Jamon Camisso
# Homepage http://wiki.scyp.atrc.utoronto.ca/wiki/WiFiPositioning
# Contact scyp@atrc.utoronto.ca
#    
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program, see enclosed file gpl-3.txt or browse to
# <http://www.gnu.org/licenses/gpl.txt>
###

import string
import sqlite3

class Scan:
	def RecordNewScan(self, buildingFloorId, scanTime, xOffset, yOffset, fixData, latitude, longitude, sateliteNumber):
		connection=sqlite3.connect("Addresses.db")
		try:
			oldScanTime = connection.execute("SELECT ScanTime FROM ScanInfos WHERE XOffset = ? AND YOffset = ? AND BuildingFloorId = ?;", (xOffset, yOffset, buildingFloorId)).fetchone()
			if (oldScanTime is not None):
				oldScanTime = oldScanTime[0]
				connection.execute("DELETE FROM ScanInfos WHERE DateTime(ScanTime) = DateTime(?);", (oldScanTime))
				connection.commit()
				connection.close()
				connection=sqlite3.connect("Addresses.db")
				connection.execute("DELETE FROM GPSScans WHERE DateTime(ScanTime) = DateTime(?);", (oldScanTime))
				connection.commit()
				connection.close()
				connection=sqlite3.connect("Addresses.db")
				connection.execute("DELETE FROM WifiScans WHERE DateTime(ScanTime) = DateTime(?);", (oldScanTime))
				connection.commit()
			connection.close()
			connection=sqlite3.connect("Addresses.db")
			connection.execute("INSERT INTO ScanInfos (BuildingFloorId, ScanTime, XOffset, YOffset) VALUES (?, ?, ?, ?);", (buildingFloorId, scanTime, xOffset, yOffset))
		except: 
			print 'Error connecting to the database...'
		connection.commit()
		connection.close()
		self.RecordNewGpsScan(fixData, latitude, longitude, sateliteNumber, scanTime)

	def RecordNewGpsScan(self, fixData, latitude, longitude, sateliteNumber, scanTime):
		connection=sqlite3.connect("Addresses.db")
		try:
			connection.execute("INSERT INTO GPSScans (FixData, Latitude, Longitude, SateliteNumber, ScanTime) VALUES (?, ?, ?, ?, ?);", (fixData, latitude, longitude, sateliteNumber, scanTime))
		except: 
			print 'Error connecting to the database...'
		connection.commit()
		connection.close()
	
	def RecordNewWifiScan(self, macId, signalStrength, scanTime, count):
		connection=sqlite3.connect("Addresses.db")
		try:
			connection.execute("INSERT INTO WiFiScans (MacId, SignalStrength, ScanTime, Count) VALUES (?, ?, ?, ?);", (macId, signalStrength, scanTime, count))
		except: 
			print 'Error connecting to the database...'
		connection.commit()
		connection.close()
